#include<iostream>
#include<cstring>
using namespace std;
class Student;
class CDate
{
private:
    int year, month, day;
    int alldays;//以1922年1月1日为基准
public:
    friend class Student;
    CDate(int ye = 0, int mo = 0, int dy = 0)
    {
        year = ye;
        month = mo;
        day = dy;
        SetAlldays();
    }
    void Set(int ye = 0, int mo = 0, int dy = 0)
    {
        year = ye;
        month = mo;
        day = dy;
        SetAlldays();
    }
    bool IsLeap(int i)
    {
        return ((i % 4 == 0) && (i % 100 != 0)) || (i % 400 == 0);
    }
    void SetAlldays()
    {
        alldays = 0;
        int mon[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
        for (int i = 1890; i < year; ++i)
        {
            if (IsLeap(i))
                alldays += 366;
            else
                alldays += 365;
        }
        if (IsLeap(year))
        {
            mon[1]++;
        }
        for (int i = 0; i < month ; ++i)
        {
            alldays += mon[i];
        }
        alldays += day;
    }
    
};
class Student
{
private:
    string name;
    CDate bir;
public:
    Student(string na = "", int ye = 0, int mo = 0, int dy = 0) :bir(ye, mo, dy)
    {
        name = na;
    }
    string getName()
    {
        return name;
    }
    int getdays()
    {
        return bir.alldays;
    }
    int operator-(Student& a)
    {
        return bir.alldays-a.bir.alldays;
    }
    void Set(string na = "", int ye = 0, int mo = 0, int dy = 0)
    {
        bir.Set(ye, mo, dy);
        name = na;
    }
    friend class CDate;
};
int main()
{
    int t;
    cin >> t;
    Student* p = new Student[t];
    for (int i = 0; i < t; ++i)
    {
        string name;
        int year, month, day;
        cin >> name >> year >> month >> day;
        p[i].Set(name, year, month, day);
    }
    int maxLeft = -1;
    string maxName = "", minName = "";
    for (int i = 0; i < t - 1; ++i)
    {
        for (int j = i + 1; j < t; ++j)
        {
            int tmpLeft = 0;
            if ((p[i] - p[j]) > 0)
            {
                tmpLeft = p[i] - p[j];
                if (tmpLeft > maxLeft)
                {
                    maxLeft = tmpLeft;
                    maxName = p[i].getName();
                    minName = p[j].getName();
                }
            }
            else
            {
                tmpLeft = p[j] - p[i];
                if (tmpLeft > maxLeft)
                {
                    maxLeft = tmpLeft;
                    maxName = p[i].getName();
                    minName = p[j].getName();
                }
            }
        }
    }
    cout << maxName <<"和"<< minName << "年龄相差最大，为" << maxLeft << "天。";
    //cout << maxName <<" hhh "<< minName << " hhhh " << maxLeft << " hhh ";
    delete[] p;
}